﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import Engine.Events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Engine.*;
    import flash.external.*;

    public class SNAPISocialNetwork extends SocialNetwork {

        protected var m_snapiUser:Object;
        protected var m_friends:Array;
        protected var m_appFriends:Array;
        protected var m_offlineMode:Boolean;
        protected var m_offlineModeId:Number;

        public function SNAPISocialNetwork(_arg1:String=null, _arg2:String=null){
            var _local6:Number;
            super();
            var _local3:String;
            var _local4:Object = GlobalEngine.getFlashVars();
            var _local5:Boolean;
            if (_local4.decouple_from_facebook){
                GlobalEngine.log("SNAPI", "Attempting to decouple from SNAPI");
                _local6 = parseFloat(_local4.decoupled_facebook_id);
                if (isNaN(_local6)){
                    GlobalEngine.log("SNAPI", "Can't decouple from SNAPI, invalid ID");
                    _local5 = false;
                } else {
                    this.m_offlineModeId = _local6;
                    _local5 = true;
                    GlobalEngine.log("SNAPI", ("Succesfully decoupled from SNAPI.  OfflineId: " + this.m_offlineModeId));
                };
            };
            if (_local5){
                this.m_offlineMode = true;
            };
        }
        public function getUser():void{
            var userData:* = null;
            var key:* = null;
            try {
                userData = ExternalInterface.call("getUserInfo");
                if (((!((userData == null))) && (userData.hasOwnProperty("zid")))){
                    this.m_snapiUser = new Object();
                    for (key in userData) {
                        this.m_snapiUser[key] = userData[key];
                    };
                };
            } catch(err:Error) {
            };
            if (((!((this.m_snapiUser == null))) && ((this.m_snapiUser.zid == 0)))){
                this.m_snapiUser = null;
            };
            if ((((this.m_snapiUser == null)) && (!(this.m_offlineMode)))){
                GlobalEngine.error("SNAPI", "Unable to load SNAPI user info from JS.");
                ErrorManager.addError("Unable to load SNAPI user info from JS.", ErrorManager.ERROR_FAILED_TO_LOAD);
            };
            dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_USER_COMPLETE));
        }
        protected function getExtendedPermissions():void{
        }
        override public function getLoggedInUserId():String{
            var _local1:String;
            if (this.m_snapiUser){
                _local1 = this.m_snapiUser.zid.toString();
            } else {
                if (this.m_offlineMode){
                    _local1 = this.m_offlineModeId.toString();
                };
            };
            return (_local1);
        }
        override public function getLoggedInUser():SocialNetworkUser{
            return (this.toSocialNetworkUser(this.m_snapiUser));
        }
        public function getFriendList():void{
            try {
                this.m_friends = ExternalInterface.call("getFriendData");
            } catch(err:Error) {
            };
            if ((((this.m_friends == null)) && ((this.m_offlineMode == false)))){
                GlobalEngine.error("SNAPI", "Unable to load friend data from JS");
                ErrorManager.addError("Unable to load SNAPI friend data from JS.", ErrorManager.ERROR_FAILED_TO_LOAD);
            };
            dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_FRIENDS_COMPLETE));
        }
        public function getAppFriends():void{
            try {
                this.m_appFriends = ExternalInterface.call("getAppFriendIds");
            } catch(err:Error) {
            };
            if ((((this.m_appFriends == null)) && ((this.m_offlineMode == false)))){
                GlobalEngine.log("SNAPI", "Unable to load appFriendIds from JS");
                ErrorManager.addError("Unable to load SNAPI appFriendIds from JS", ErrorManager.ERROR_FAILED_TO_LOAD);
            };
            dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_APP_FRIENDS_COMPLETE));
        }
        override public function getFriendUsers():Array{
            var _local2:Array;
            var _local3:int;
            var _local4:SocialNetworkUser;
            var _local1:Array = [];
            if (this.m_friends){
                _local2 = this.m_friends;
                _local3 = 0;
                while (_local3 < _local2.length) {
                    _local4 = this.toSocialNetworkUser(_local2[_local3]);
                    _local1.push(_local4);
                    _local3++;
                };
            };
            return (_local1);
        }
        override public function getAppFriendsIds():Array{
            return (this.m_appFriends);
        }
        override public function streamPublish(_arg1:Object, _arg2:Object, _arg3:String, _arg4:String=null, _arg5:Function=null, _arg6:Boolean=false, _arg7:String=""):void{
            throw (new Error("Cannot use streamPublish with SNAPI, please use snapiStreamPublish"));
        }
        public function snapiStreamPublish(_arg1:Object):void{
            var data:* = _arg1;
            try {
                if (ExternalInterface.available){
                    if (GlobalEngine.stage.displayState == StageDisplayState.FULL_SCREEN){
                        GlobalEngine.stage.displayState = StageDisplayState.NORMAL;
                    };
                    ExternalInterface.call("snapiStreamPublish", data);
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        public function snapiAwardXp(_arg1:Number):void{
            var amount:* = _arg1;
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call("snapiAwardXp", amount);
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        override public function showStreamPermissions(_arg1:Function=null):void{
            var callback = _arg1;
            super.showStreamPermissions(callback);
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call("showStreamPermissions");
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        override public function showBookmarkDialog():void{
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call("showBookmarkDialog");
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        override public function showEmailPermission(_arg1:Function=null):void{
            var callback = _arg1;
            super.showEmailPermission(callback);
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call("showEmailPermission");
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        protected function toSocialNetworkUser(_arg1:Object):SocialNetworkUser{
            var _local2:SocialNetworkUser;
            if (this.m_offlineMode){
                _local2 = new SocialNetworkUser(this.m_offlineModeId, Constants.NO_NETWORK);
                _local2.firstName = "Decoupled";
                _local2.name = "Decoupled";
                _local2.picture = "";
            } else {
                _local2 = new SocialNetworkUser(_arg1.zid);
                _local2.firstName = _arg1.first_name;
                _local2.name = _arg1.name;
                _local2.picture = _arg1.pic_square;
                _local2.gender = _arg1.sex;
                _local2.locale = _arg1.locale;
            };
            return (_local2);
        }

    }
}//package Engine.Classes 
